#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <exception>
#include <cstring>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>

using namespace std;

typedef struct TTerm {
	int type;
	string value;
	vector<TTerm> parts;
} TTerm;

class MyException : public exception {
public:
	string info;
	string line;

	virtual ~MyException(void) throw() {}
};

const int BNFWORD = 0;
const int ADDOP = 1;
const int ASSIGNMENT = 2;
const int DIGIT = 3;
const int DIGITS = 4;
const int EXPRESSION = 5;
const int FACTOR = 6;
const int FACTORS = 7;
const int FACTORS_TAIL = 8;
const int LETTER = 9;
const int LETTERDIGITS = 10;
const int LINE = 11;
const int MULOP = 12;
const int MULTIPLE_LINES = 13;
const int MYFILE = 14;
const int NUMBER = 15;
const int PRINT = 16;
const int RESET = 17;
const int TERM = 18;
const int TERMS = 19;
const int TERMS_TAIL = 20;
const int VAR = 21;

string trim(string s) {
	int begin = s.find_first_not_of(" \t\r\n");
	int end = s.find_last_not_of(" \t\r\n");
	if (begin == -1) return "";
	return s.substr(begin, end+1-begin);
}

TTerm matchBNFWord(const string word, string& line){
	TTerm BNFWord;
	BNFWord.type = BNFWORD;

	if (line.length() < word.length()) {
		MyException e;
		e.info = "too short line in BNFWord";
		e.line = line;
		throw e;
	}
	for (unsigned int i=0; i<word.length(); ++i) {
		if (word[i] != line[i]) {
			MyException e;
			e.info = "mismatch in BNFWord";
			e.line = line;
			throw e;
		}
	}
	line = line.substr(word.length(), line.length()-word.length());
	BNFWord.value = word;

	return BNFWord;
}

void needSeparator(string& line) {
	if (line.length()>0 && line[0]!=' ') {
		MyException e;
		e.info = "need separator";
		e.line = line;
		throw e;
	}
	line = trim(line);
}

TTerm match_addop(string& line);
TTerm match_assignment(string& line);
TTerm match_digit(string& line);
TTerm match_digits(string& line);
TTerm match_expression(string& line);
TTerm match_factor(string& line);
TTerm match_factors(string& line);
TTerm match_factors_tail(string& line);
TTerm match_letter(string& line);
TTerm match_letterdigits(string& line);
TTerm match_line(string& line);
TTerm match_mulop(string& line);
TTerm match_multiple_lines(string& line);
TTerm match_myfile(string& line);
TTerm match_number(string& line);
TTerm match_print(string& line);
TTerm match_reset(string& line);
TTerm match_term(string& line);
TTerm match_terms(string& line);
TTerm match_terms_tail(string& line);
TTerm match_var(string& line);

TTerm match_myfile(string& line) {
	TTerm _myfile;
	TTerm t;
	_myfile.type = MYFILE;

	t = match_multiple_lines(line);
	_myfile.parts.push_back(t);

	t = matchBNFWord("#", line);
	_myfile.parts.push_back(t);

	return _myfile;
}

TTerm match_multiple_lines(string& line) {
	TTerm _multiple_lines;
	TTerm t;
	_multiple_lines.type = MULTIPLE_LINES;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _multiple_lines.parts;
		try {
			t = match_line(line);
			_multiple_lines.parts.push_back(t);

			t = match_multiple_lines(line);
			_multiple_lines.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_multiple_lines.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _multiple_lines.parts;
		try {
			t = match_line(line);
			_multiple_lines.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_multiple_lines.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in MULTIPLE_LINES";
		myE.line = line;
		throw myE;
	}
	return _multiple_lines;
}

TTerm match_line(string& line) {
	TTerm _line;
	TTerm t;
	_line.type = LINE;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _line.parts;
		try {
			hasErr = true;
			bakLn[1] = line;
			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _line.parts;
				try {
					t = match_assignment(line);
					_line.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_line.parts = bakParts;
				}
			}

			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _line.parts;
				try {
					t = match_print(line);
					_line.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_line.parts = bakParts;
				}
			}

			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _line.parts;
				try {
					t = match_reset(line);
					_line.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_line.parts = bakParts;
				}
			}

			if (hasErr) {
				line = bakLn[1];
				myE.info = "mismatch in LINE";
				myE.line = line;
				throw myE;
			}
			t = matchBNFWord(";", line);
			_line.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_line.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _line.parts;
		try {
			t = matchBNFWord(";", line);
			_line.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_line.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in LINE";
		myE.line = line;
		throw myE;
	}
	return _line;
}

TTerm match_assignment(string& line) {
	TTerm _assignment;
	TTerm t;
	_assignment.type = ASSIGNMENT;

	t = match_var(line);
	_assignment.parts.push_back(t);

	t = matchBNFWord(":=", line);
	_assignment.parts.push_back(t);

	t = match_expression(line);
	_assignment.parts.push_back(t);

	return _assignment;
}

TTerm match_print(string& line) {
	TTerm _print;
	TTerm t;
	_print.type = PRINT;

	t = matchBNFWord("PRINT", line);
	_print.parts.push_back(t);

	needSeparator(line);

	t = match_var(line);
	_print.parts.push_back(t);

	return _print;
}

TTerm match_reset(string& line) {
	TTerm _reset;
	TTerm t;
	_reset.type = RESET;

	t = matchBNFWord("RESET", line);
	_reset.parts.push_back(t);

	return _reset;
}

TTerm match_expression(string& line) {
	TTerm _expression;
	TTerm t;
	_expression.type = EXPRESSION;

	t = match_terms(line);
	_expression.parts.push_back(t);

	return _expression;
}

TTerm match_terms(string& line) {
	TTerm _terms;
	TTerm t;
	_terms.type = TERMS;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _terms.parts;
		try {
			t = match_term(line);
			_terms.parts.push_back(t);

			t = match_terms_tail(line);
			_terms.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_terms.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _terms.parts;
		try {
			t = match_term(line);
			_terms.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_terms.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in TERMS";
		myE.line = line;
		throw myE;
	}
	return _terms;
}

TTerm match_terms_tail(string& line) {
	TTerm _terms_tail;
	TTerm t;
	_terms_tail.type = TERMS_TAIL;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _terms_tail.parts;
		try {
			t = match_addop(line);
			_terms_tail.parts.push_back(t);

			t = match_term(line);
			_terms_tail.parts.push_back(t);

			t = match_terms_tail(line);
			_terms_tail.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_terms_tail.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _terms_tail.parts;
		try {
			t = match_addop(line);
			_terms_tail.parts.push_back(t);

			t = match_term(line);
			_terms_tail.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_terms_tail.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in TERMS_TAIL";
		myE.line = line;
		throw myE;
	}
	return _terms_tail;
}

TTerm match_term(string& line) {
	TTerm _term;
	TTerm t;
	_term.type = TERM;

	t = match_factors(line);
	_term.parts.push_back(t);

	return _term;
}

TTerm match_factors(string& line) {
	TTerm _factors;
	TTerm t;
	_factors.type = FACTORS;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factors.parts;
		try {
			t = match_factor(line);
			_factors.parts.push_back(t);

			t = match_factors_tail(line);
			_factors.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factors.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factors.parts;
		try {
			t = match_factor(line);
			_factors.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factors.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in FACTORS";
		myE.line = line;
		throw myE;
	}
	return _factors;
}

TTerm match_factors_tail(string& line) {
	TTerm _factors_tail;
	TTerm t;
	_factors_tail.type = FACTORS_TAIL;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factors_tail.parts;
		try {
			t = match_mulop(line);
			_factors_tail.parts.push_back(t);

			t = match_factor(line);
			_factors_tail.parts.push_back(t);

			t = match_factors_tail(line);
			_factors_tail.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factors_tail.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factors_tail.parts;
		try {
			t = match_mulop(line);
			_factors_tail.parts.push_back(t);

			t = match_factor(line);
			_factors_tail.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factors_tail.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in FACTORS_TAIL";
		myE.line = line;
		throw myE;
	}
	return _factors_tail;
}

TTerm match_factor(string& line) {
	TTerm _factor;
	TTerm t;
	_factor.type = FACTOR;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factor.parts;
		try {
			t = matchBNFWord("(", line);
			_factor.parts.push_back(t);

			t = match_expression(line);
			_factor.parts.push_back(t);

			t = matchBNFWord(")", line);
			_factor.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factor.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factor.parts;
		try {
			t = match_var(line);
			_factor.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factor.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _factor.parts;
		try {
			t = match_number(line);
			_factor.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_factor.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in FACTOR";
		myE.line = line;
		throw myE;
	}
	return _factor;
}

TTerm match_addop(string& line) {
	TTerm _addop;
	TTerm t;
	_addop.type = ADDOP;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _addop.parts;
		try {
			t = matchBNFWord("+", line);
			_addop.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_addop.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _addop.parts;
		try {
			t = matchBNFWord("-", line);
			_addop.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_addop.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in ADDOP";
		myE.line = line;
		throw myE;
	}
	return _addop;
}

TTerm match_mulop(string& line) {
	TTerm _mulop;
	TTerm t;
	_mulop.type = MULOP;

	t = matchBNFWord("*", line);
	_mulop.parts.push_back(t);

	return _mulop;
}

TTerm match_var(string& line) {
	TTerm _var;
	TTerm t;
	_var.type = VAR;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _var.parts;
		try {
			t = match_letter(line);
			_var.parts.push_back(t);

			t = match_letterdigits(line);
			_var.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_var.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _var.parts;
		try {
			t = match_letter(line);
			_var.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_var.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in VAR";
		myE.line = line;
		throw myE;
	}
	return _var;
}

TTerm match_letterdigits(string& line) {
	TTerm _letterdigits;
	TTerm t;
	_letterdigits.type = LETTERDIGITS;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _letterdigits.parts;
		try {
			hasErr = true;
			bakLn[1] = line;
			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _letterdigits.parts;
				try {
					t = match_letter(line);
					_letterdigits.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_letterdigits.parts = bakParts;
				}
			}

			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _letterdigits.parts;
				try {
					t = match_digit(line);
					_letterdigits.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_letterdigits.parts = bakParts;
				}
			}

			if (hasErr) {
				line = bakLn[1];
				myE.info = "mismatch in LETTERDIGITS";
				myE.line = line;
				throw myE;
			}
			t = match_letterdigits(line);
			_letterdigits.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_letterdigits.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _letterdigits.parts;
		try {
			hasErr = true;
			bakLn[1] = line;
			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _letterdigits.parts;
				try {
					t = match_letter(line);
					_letterdigits.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_letterdigits.parts = bakParts;
				}
			}

			if (hasErr) {
				hasErr = false;
				line = bakLn[1];
				bakParts = _letterdigits.parts;
				try {
					t = match_digit(line);
					_letterdigits.parts.push_back(t);

				}
				catch(MyException e){
					hasErr = true;
					myE = e;
					_letterdigits.parts = bakParts;
				}
			}

			if (hasErr) {
				line = bakLn[1];
				myE.info = "mismatch in LETTERDIGITS";
				myE.line = line;
				throw myE;
			}
		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_letterdigits.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in LETTERDIGITS";
		myE.line = line;
		throw myE;
	}
	return _letterdigits;
}

TTerm match_number(string& line) {
	TTerm _number;
	TTerm t;
	_number.type = NUMBER;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _number.parts;
		try {
			t = match_digits(line);
			_number.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_number.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _number.parts;
		try {
			t = matchBNFWord("-", line);
			_number.parts.push_back(t);

			t = match_digits(line);
			_number.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_number.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in NUMBER";
		myE.line = line;
		throw myE;
	}
	return _number;
}

TTerm match_digits(string& line) {
	TTerm _digits;
	TTerm t;
	_digits.type = DIGITS;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _digits.parts;
		try {
			t = match_digit(line);
			_digits.parts.push_back(t);

			t = match_digits(line);
			_digits.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_digits.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _digits.parts;
		try {
			t = match_digit(line);
			_digits.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_digits.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in DIGITS";
		myE.line = line;
		throw myE;
	}
	return _digits;
}

TTerm match_letter(string& line) {
	TTerm _letter;
	TTerm t;
	_letter.type = LETTER;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;

	char letterCh[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	if (('A'<=line[0]&&line[0]<='Z')||('a'<=line[0]&&line[0]<='z')) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _letter.parts;

		string s = " ";
		s[0] = line[0];
		t = matchBNFWord(s, line);
		_letter.parts.push_back(t);
	}
	else {
		hasErr = true;
		_letter.parts = bakParts;
	}

// 	for (int i=0; i<strlen(letterCh); ++i) {
// 		if (hasErr) {
// 			hasErr = false;
// 			line = bakLn[0];
// 			bakParts = _letter.parts;
// 			try {
// 				string s = " ";
// 				s[0] = letterCh[i];
// 				t = matchBNFWord(s, line);
// 				_letter.parts.push_back(t);
// 
// 			}
// 			catch(MyException e){
// 				hasErr = true;
// 				myE = e;
// 				_letter.parts = bakParts;
// 			}
// 		}
// 	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in LETTER";
		myE.line = line;
		throw myE;
	}
	return _letter;
}

TTerm match_digit(string& line) {
	TTerm _digit;
	TTerm t;
	_digit.type = DIGIT;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;

	if ('0'<=line[0]&&line[0]<='9') {
		hasErr = false;
		line = bakLn[0];
		bakParts = _digit.parts;

		string s = " ";
		s[0] = line[0];
		t = matchBNFWord(s, line);
		_digit.parts.push_back(t);
	}
	else {
		hasErr = true;
		_digit.parts = bakParts;
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in DIGIT";
		myE.line = line;
		throw myE;
	}
	return _digit;
}

typedef struct TBigInt{
	string val;
} TBigInt;

char intX[100];
char intY[100];
char intZ[100];

TBigInt operator-(TBigInt& a, TBigInt& b);

// TBigInt operator+(TBigInt& a, TBigInt& b) {
// 	TBigInt r;
// 	if (a.val == "UNDEF" || b.val == "UNDEF") {
// 		r.val = "UNDEF";
// 		return r;
// 	}
// 
// 	long long x, y, z;
// 	x = y = 0;
// 	for (int i=0; i<a.val.length(); ++i) {
// 		if (a.val[i]=='-') continue;
// 		x = x*10+a.val[i]-'0';
// 	}
// 	for (int i=0; i<b.val.length(); ++i) {
// 		if (b.val[i]=='-') continue;
// 		y = y*10+b.val[i]-'0';
// 	}
// 
// 	if (a.val[0]=='-') x = -x;
// 	if (b.val[0]=='-') y = -y;
// 
// 	z = x + y;
// 	sprintf(intZ, "%lld", z);
// 	r.val = string(intZ);
// 	return r;
// }
// TBigInt operator-(TBigInt& a, TBigInt& b) {
// 	TBigInt r;
// 	if (a.val == "UNDEF" || b.val == "UNDEF") {
// 		r.val = "UNDEF";
// 		return r;
// 	}
// 
// 	long long x, y, z;
// 	x = y = 0;
// 	for (int i=0; i<a.val.length(); ++i) {
// 		if (a.val[i]=='-') continue;
// 		x = x*10+a.val[i]-'0';
// 	}
// 	for (int i=0; i<b.val.length(); ++i) {
// 		if (b.val[i]=='-') continue;
// 		y = y*10+b.val[i]-'0';
// 	}
// 
// 	if (a.val[0]=='-') x = -x;
// 	if (b.val[0]=='-') y = -y;
// 
// 	z = x - y;
// 	sprintf(intZ, "%lld", z);
// 	r.val = string(intZ);
// 	return r;
// }
// TBigInt operator*(TBigInt& a, TBigInt& b) {
// 	TBigInt r;
// 	if (a.val == "UNDEF" || b.val == "UNDEF") {
// 		r.val = "UNDEF";
// 		return r;
// 	}
// 
// 	long long x, y, z;
// 	x = y = 0;
// 	for (int i=0; i<a.val.length(); ++i) {
// 		if (a.val[i]=='-') continue;
// 		x = x*10+a.val[i]-'0';
// 	}
// 	for (int i=0; i<b.val.length(); ++i) {
// 		if (b.val[i]=='-') continue;
// 		y = y*10+b.val[i]-'0';
// 	}
// 
// 	if (a.val[0]=='-') x = -x;
// 	if (b.val[0]=='-') y = -y;
// 
// 	z = x * y;
// 	sprintf(intZ, "%lld", z);
// 	r.val = string(intZ);
// 	return r;
// }


TBigInt operator+(TBigInt& a, TBigInt& b) {
	TBigInt r;
	if (a.val == "UNDEF" || b.val == "UNDEF") {
		r.val = "UNDEF";
		return r;
	}

	bool negA, negB;
	negA = (a.val[0]=='-');
	negB = (b.val[0]=='-');
	if (negA && !negB) {
		TBigInt na = a;
		na.val = na.val.substr(1,na.val.length()-1);
		TBigInt tbi = na-b;
		if (tbi.val[0]=='-')
			r.val = tbi.val.substr(1,tbi.val.length()-1);
		else
			r.val = "-"+tbi.val;
		return r;
	}
	else if (!negA && negB) {
		TBigInt nb = b;
		nb.val = nb.val.substr(1,nb.val.length()-1);
		TBigInt tbi = a-nb;
		r = tbi;
		return r;
	}

	memset(intX, 0, 100);
	memset(intY, 0, 100);
	memset(intZ, 0, 100);

	int lx, ly;
	lx = a.val.length();
	ly = b.val.length();
	strcpy(intX, a.val.c_str());
	strcpy(intY, b.val.c_str());
	reverse(intX, intX+lx);
	reverse(intY, intY+ly);
	if (negA) {
		--lx;--ly;
		intX[lx]=intY[ly]=0;
	}

	for (int i=0; i<lx; ++i)
		intX[i] = intX[i] - '0';
	for (int i=0; i<ly; ++i)
		intY[i] = intY[i] - '0';

	for (int i=0; i<max(lx,ly)+1; ++i) {
		intZ[i] += intX[i]+intY[i];
		intZ[i+1] = intZ[i]/10;
		intZ[i] %= 10;
	}

	bool addZero = false;
	for (int i=99;i>=0;--i) {
		if (intZ[i]>0) {
			addZero = true;
		}
		if (addZero) intZ[i] = intZ[i]+'0';
	}
	if (!addZero) intZ[0] = '0';

	string s(intZ);
	reverse(s.begin(), s.end());

	if (negA) r.val = "-"+s;
	else r.val = s;
	return r;
}

TBigInt operator-(TBigInt& a, TBigInt& b) {
	TBigInt r;
	if (a.val == "UNDEF" || b.val == "UNDEF") {
		r.val = "UNDEF";
		return r;
	}

	bool negA, negB;
	negA = (a.val[0]=='-');
	negB = (b.val[0]=='-');
	if (negA && !negB) {
		TBigInt na = a;
		na.val = na.val.substr(1,na.val.length()-1);
		TBigInt tbi = na+b;
		if (tbi.val[0]=='-')
			r.val = tbi.val.substr(1,tbi.val.length()-1);
		else
			r.val = "-"+tbi.val;
		return r;
	}
	else if (!negA && negB) {
		TBigInt nb = b;
		nb.val = nb.val.substr(1,nb.val.length()-1);
		TBigInt tbi = a+nb;
		r = tbi;
		return r;
	}

	memset(intX, 0, 100);
	memset(intY, 0, 100);
	memset(intZ, 0, 100);

	int lx, ly;
	lx = a.val.length();
	ly = b.val.length();
	strcpy(intX, a.val.c_str());
	strcpy(intY, b.val.c_str());
	reverse(intX, intX+lx);
	reverse(intY, intY+ly);
	if (negB) {
		--lx;--ly;
		intX[lx]=intY[ly]=0;
	}

	for (int i=0; i<lx; ++i)
		intX[i] = intX[i] - '0';
	for (int i=0; i<ly; ++i)
		intY[i] = intY[i] - '0';

	bool xSmaller = false;
	if (lx<ly) {
		xSmaller = true;
	}
	else if (lx == ly) {
		for (int i=0; i<lx; ++i)
			if (intX[i]<intY[i]) {
				xSmaller = true;
				break;
			}
			else if (intX[i]>intY[i]) break;
	}

	for (int i=0; i<max(lx,ly); ++i) {
		intZ[i] += (xSmaller)?(intY[i]-intX[i]):(intX[i]-intY[i]);
		while (intZ[i]<0) {
			intZ[i] += 10;
			--intZ[i+1];
		}
	}

	bool addZero = false;
	for (int i=99;i>=0;--i) {
		if (intZ[i]>0) {
			addZero = true;
		}
		if (addZero) intZ[i] = intZ[i]+'0';
	}
	if (!addZero) intZ[0] = '0';

	string s(intZ);
	reverse(s.begin(), s.end());
	if (negA&&!xSmaller) r.val="-"+s;
	else if (!negA&&xSmaller) r.val="-"+s;
	else r.val = s;

	return r;
}
TBigInt operator*(TBigInt& a, TBigInt& b) {
	TBigInt r;
	if (a.val == "UNDEF" || b.val == "UNDEF") {
		r.val = "UNDEF";
		return r;
	}

	memset(intX, 0, 100);
	memset(intY, 0, 100);
	memset(intZ, 0, 100);

	bool negA, negB;
	negA = (a.val[0]=='-');
	negB = (b.val[0]=='-');

	int lx, ly;
	lx = a.val.length();
	ly = b.val.length();
	strcpy(intX, a.val.c_str());
	strcpy(intY, b.val.c_str());
	reverse(intX, intX+lx);
	reverse(intY, intY+ly);
	if (negA) --lx;
	if (negB) --ly;

	for (int i=0; i<lx; ++i)
		intX[i] = intX[i] - '0';
	for (int i=0; i<ly; ++i)
		intY[i] = intY[i] - '0';

	int nl = 0;
	for (int i=0; i<lx; ++i) {
		for (int j=0; j<ly; ++j) {
			intZ[i+j] += intX[i]*intY[j];
			intZ[i+j+1] += intZ[i+j]/10;
			intZ[i+j] %= 10;
		}
	}
	nl = lx+ly-1;
	while (intZ[nl]>=10) {
		intZ[nl+1] += intZ[nl]/10;
		intZ[nl] %= 10;
		++nl;
	}

	bool addZero = false;
	for (int i=99;i>=0;--i) {
		if (intZ[i]>0) {
			addZero = true;
		}
		if (addZero) intZ[i] = intZ[i]+'0';
	}
	if (!addZero) intZ[0] = '0';

	string s(intZ);
	reverse(s.begin(), s.end());
	if (negA && !negB) s = "-"+s;
	if (!negA && negB) s = "-"+s;
	r.val = s;
	return r;
}

const int BOTH = 0;
const int INTVAL = 1;
const int VALNAME = 2;
typedef struct TVar {
	int type;
	TBigInt bigInt;
	string name;
} TVar;

map<string, TTerm> valTable;
set<string> evaluating;
TTerm undefTerm;
const int UNDEF = 1000;

TVar eval(TTerm t, TVar *param = NULL) {
	TVar res1, res2, res3;

	switch (t.type) {
	case MYFILE:
	case MULTIPLE_LINES:
	case LINE:
		for (int i=0; i<t.parts.size(); ++i)
			eval(t.parts[i]);
		break;
	case ASSIGNMENT:
		res1 = eval(t.parts[0]);
		valTable[res1.name] = t.parts[2];
		break;
	case PRINT:
		res1 = eval(t.parts[1]);
		cout << eval(valTable[res1.name]).bigInt.val << endl;
		break;
	case RESET:
		valTable.clear();
		break;
	case EXPRESSION:
	case TERM:
		return eval(t.parts[0]);
		break;
	case TERMS:
	case FACTORS:
		if (t.parts.size()==1) {
			return eval(t.parts[0]);
		}
		else {
			res1 = eval(t.parts[0]);
			res2 = eval(t.parts[1], &res1);
			res3.type = 1;
			res3.bigInt = res2.bigInt;
			return res3;
		}
		break;
	case TERMS_TAIL:
	case FACTORS_TAIL:
		if (t.parts.size() == 2) {
			res1 = *param;
			res2 = eval(t.parts[1]);
			res3.type = 1;
			if (t.parts[0].parts[0].value == "+") {
				res3.bigInt = res1.bigInt + res2.bigInt;
			}
			else if (t.parts[0].parts[0].value == "-") {
				res3.bigInt = res1.bigInt - res2.bigInt;
			}
			else if (t.parts[0].parts[0].value == "*") {
				res3.bigInt = res1.bigInt * res2.bigInt;
			}
			return res3;
		}
		else {
			res1 = *param;
			res2 = eval(t.parts[1]);
			res3.type = 1;
			if (t.parts[0].parts[0].value == "+") {
				res3.bigInt = res1.bigInt + res2.bigInt;
			}
			else if (t.parts[0].parts[0].value == "-") {
				res3.bigInt = res1.bigInt - res2.bigInt;
			}
			else if (t.parts[0].parts[0].value == "*") {
				res3.bigInt = res1.bigInt * res2.bigInt;
			}
			res2 = eval(t.parts[2], &res3);
			res3.bigInt = res2.bigInt;
			return res3;
		}
		break;
	case FACTOR:
		if (t.parts.size()>1)
			return eval(t.parts[1]);
		return eval(t.parts[0]);
		break;
	case VAR:
		res1 = eval(t.parts[0]);
		if (t.parts.size()>1) res2 = eval(t.parts[1]);
		else res2.name = "";
		res3.type = 0;
		res3.name = res1.name + res2.name;
		if (valTable.count(res3.name) == 0){
			valTable[res3.name] = undefTerm;
		}
		if (evaluating.count(res3.name) > 0) {
			res3.bigInt.val = "UNDEF";
		}
		else {
			evaluating.insert(res3.name);
			res3.bigInt = eval(valTable[res3.name]).bigInt;
			evaluating.erase(res3.name);
		}
		return res3;
		break;
	case LETTERDIGITS:
		res1 = eval(t.parts[0]);
		if (t.parts.size()>1) res2 = eval(t.parts[1]);
		else res2.name = "";
		res3.type = 2;
		res3.name = res1.name + res2.name;
		return res3;
		break;
	case NUMBER:
	case DIGITS:
		res1 = eval(t.parts[0]);
		if (t.parts.size()>1) res2 = eval(t.parts[1]);
		else res2.name = "";
		res3.type = 1;
		res3.bigInt.val = res1.bigInt.val + res2.bigInt.val;
		return res3;
		break;
	case LETTER:
		return eval(t.parts[0]);
		break;
	case DIGIT:
		res1 = eval(t.parts[0]);
		res3.type = 1;
		res3.bigInt.val = res1.name;
		return res3;
		break;
	case BNFWORD:
		res3.type = 2;
		res3.name = t.value;
		res3.bigInt.val = t.value;
		return res3;
		break;
	case UNDEF:
		res3.type = 1;
		res3.bigInt.val = "UNDEF";
		return res3;
		break;
	}

	return res3;
}

int main(void) {
	char line[1200];
	string fileString="";
	while (true) {
		memset(line, 0, 1200);
		if (cin.eof()) break;
		cin.getline(line, 1200);
		string s(line);
		s = trim(s);
		string nospace="";
		for (int i=0; i<s.length(); ++i) {
			if (s[i] != ' ') nospace += s[i];
		}
		nospace += ";";
		if (nospace.find(":=") == string::npos) {
			if (nospace.substr(0,5) == "PRINT") {
				fileString += nospace.substr(0,5) + " ";
				fileString += nospace.substr(5,nospace.length()-5);
				continue;
			}
		}
		fileString += nospace;
	}
	fileString += "#";
	undefTerm.type = UNDEF;

	try {
		string s = fileString;
		TTerm t = match_myfile(s);
		
		if (s.length() == 0) {
			eval(t);
		}
	}
	catch (...) {
// 		while (1);
	}
	return 0;
}
